<?php

namespace App\Http\Controllers\Api;

use App\Exports\SalaryExport;
use App\Exports\SalaryInfoExport;
use App\Exports\TestExport;
use App\Http\Controllers\Controller;
use App\Rep\SalaryRep;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
class Salary extends Controller
{
    //薪资列表
    protected $rep;

    public function __construct(Request $request)
    {
        $this->rep = new SalaryRep();
    }

    /**
     * 获取薪资列表
     */
    public function getSalaryList(Request $request){
        $params = $request->all();
        //订单日期（默认当月）
        //$params['start_time'] = $params['start_time']??date("Y-m-01");
        //$params['end_time'] = $params['end_time']??date("Y-m-d",strtotime($params['start_time']." +1 month -1 day"));
        $export = $request->get('export')??0;
        if($export == 1){
            try{
                $filename = $params['start_time'].'-'.$params['end_time'].'薪资列表.xlsx';
                $export = new SalaryExport($params);
                //$export = new TestExport($params);
                return Excel::download($export,$filename,\Maatwebsite\Excel\Excel::XLSX,[
                    'Content-Type'=> 'application/octet-stream',
                    'Content-Disposition'=> 'attachment; filename='.$filename,
                    'Content-Transfer-Encoding'=> 'binary',
                ]);
            }catch(\Exception $exception){
                return re(0,[],$exception->getMessage());
            }
        }
        $data = $this->rep->getSalaryListRep($params);
        return re(0,$data,'success');
    }

    /**
     * 获取员工薪资详情
     * @param Request $request
     */
    public function getSalaryInfo(Request $request){
        $params = $request->all();
        //订单日期（默认当月）
        $params['start_time'] = $params['start_time']??date("Y-m-01");
        $params['end_time'] = $params['end_time']??date("Y-m-d",strtotime($params['start_time']." +1 month -1 day"));
        $export = $request->get('export')??0;
        if($export == 1){
            try{
                $filename = $params['employee_user_name'].'薪资详情.xlsx';
                $export = new SalaryInfoExport($params);
                //$export = new TestExport($params);
                return Excel::download($export,$filename,\Maatwebsite\Excel\Excel::XLSX,[
                    'Content-Type'=> 'application/octet-stream',
                    'Content-Disposition'=> 'attachment; filename='.$filename,
                    'Content-Transfer-Encoding'=> 'binary',
                ]);
            }catch(\Exception $exception){
                return re(0,[],$exception->getMessage());
            }
        }
        $data = $this->rep->getSalaryInfoRep($params);
        return re(0,$data,'success');
    }

    /**
     * 修改员工工资
     * @param Request $request
     */
    public function changeSalary(Request $request){
        $params = $request->all();
        $data = $this->rep->changeSalaryRep($params);
        return re(0,$data,'success');
    }

}
